//
//  LockView.m
//  Touch
//
//  Created by cocoazxj@163.com on 15/4/20.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "LockView.h"

@interface LockView()
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, assign) CGPoint currentPoint;
@end

@implementation LockView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib
{
    [super awakeFromNib];
    CGFloat spaceX = 20.0;
    CGFloat spaceY = 20.0;
    for(int i = 0; i < 9; i++)
    {
        int x = i%3;
        int y = i/3;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(spaceX*(x+1)+ 50*x, spaceY*(y+1)+y*50, 50, 50)];
        [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        [btn setUserInteractionEnabled:NO];
        [btn setTag:100+i];
        [self addSubview:btn];
    }
}

- (void)layoutSubviews
{
//    [super layoutSubviews];
//    CGFloat spaceX = 20.0;
//    CGFloat spaceY = 20.0;
//    for(int i = 0; i < 9; i++)
//    {
//        int x = i%3;
//        int y = i/3;
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [btn setFrame:CGRectMake(spaceX*(x+1)+ 50*x, spaceY*(y+1)+y*50, 50, 50)];
//        [btn setUserInteractionEnabled:NO];
//        [btn setTag:100+i];
//        [self addSubview:btn];
//    }
}

- (NSMutableArray *)array
{
    if(!_array)
    {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
     for(int i = 0; i < self.subviews.count; i++)
    {
        UIButton *btn = self.subviews[i];
        if(CGRectContainsPoint(btn.frame, point)) {
            if (!btn.selected) {
                btn.selected = YES;
                [self.array addObject:btn];
            }
        }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    for(int i = 0; i < self.subviews.count; i++)
    {
        UIButton *btn = self.subviews[i];
        if(CGRectContainsPoint(btn.frame, point))
        {
            // 两个连成直线的UIButton,中间不能有未连接的。
            // 取出上数组中最后一个Button 和下一个进行比较
            UIButton *before = [self.array lastObject];
            for(UIButton *b in self.subviews)
            {
                CGPoint point = b.center;
                if(point.x == (before.center.x + btn.center.x)*0.5 && point.y == (before.center.y + btn.center.y)*0.5)
                {
                    NSLog(@"--------两个button中不能用空格");
                    if (!b.selected) {
                        b.selected = YES;
                        [self.array addObject:b];
                    }
                }
            }
            if (!btn.selected) {
                btn.selected = YES;
                [self.array addObject:btn];
            }
        }
        
    }
    self.currentPoint = point;
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSMutableString *password = [NSMutableString string];
    for(UIButton *btn in self.array)
    {
        [btn setSelected:NO];
        [password appendFormat:@"%d",btn.tag];
    }
    NSLog(@"-%@-----------password",password);
    [self.array removeAllObjects];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    NSLog(@"------------%d",self.array.count);
    for(int i = 0; i < self.array.count; i++)
    {
        UIButton *btn = self.array[i];
        if(i == 0)
        {
            CGContextMoveToPoint(context, btn.center.x, btn.center.y);
        }
        else
        {
            CGContextAddLineToPoint(context, btn.center.x, btn.center.y);
        }
    }
    if(self.array.count != 0)
    {
        NSLog(@"存在");
        CGContextAddLineToPoint(context, self.currentPoint.x, self.currentPoint.y);
    }
    
    CGContextStrokePath(context);
}

@end
